package com.sjsm.sjsmscenerecord.adapter;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sjsm.sjsmscenerecord.R;
import com.sjsm.sjsmscenerecord.bean.Recorder;
import com.sjsm.sjsmscenerecord.constant.Constant;

import java.util.List;

public class RecorderAdapter extends ArrayAdapter<Recorder> {


    private LayoutInflater inflater;

    private int mMinItemWith;// 设置对话框的最大宽度和最小宽度
    private int mMaxItemWith;

    public RecorderAdapter(Context context, List<Recorder> dataList) {
        super(context, -1, dataList);
        // TODO Auto-generated constructor stub
        inflater = LayoutInflater.from(context);

        // 获取系统宽度
        WindowManager wManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wManager.getDefaultDisplay().getMetrics(outMetrics);
        mMaxItemWith = (int) (outMetrics.widthPixels * 0.7f);
        mMinItemWith = (int) (outMetrics.widthPixels * 0.15f);
    }

    @Override
    public int getViewTypeCount() {
        return 5;
    }

    @Override
    public int getItemViewType(int position) {
        return getItem(position).getViewType();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        switch (getItemViewType(position)) {
            case Constant.VOICE:
                //语音
                VoiceViewHolder viewHolder = null;
                if (convertView == null) {
                    convertView = inflater.inflate(R.layout.luyin_item_layout, parent, false);
                    viewHolder = new VoiceViewHolder();
                    viewHolder.seconds = (TextView) convertView.findViewById(R.id.recorder_time);
                    viewHolder.length = convertView.findViewById(R.id.recorder_length);

                    convertView.setTag(viewHolder);
                } else {
                    viewHolder = (VoiceViewHolder) convertView.getTag();
                }


                viewHolder.seconds.setText(Math.round(getItem(position).getTime()) + "\"");
                //设置固定宽度
                //		ViewGroup.LayoutParams lParams=viewHolder.length.getLayoutParams();
                //		lParams.width=(int) (mMinItemWith+mMaxItemWith/60f*getItem(position).getTime());
                //		viewHolder.length.setLayoutParams(lParams);
            break;
            case Constant.VIDEO:
                //视频
                VideoViewHolder videoViewHolder;
                if (convertView == null) {
                    convertView = inflater.inflate(R.layout.video_item_layout, parent, false);
                    videoViewHolder = new VideoViewHolder();
                    videoViewHolder.iv_video = (ImageView) convertView.findViewById(R.id.iv_video_first);
                    convertView.setTag(videoViewHolder);
                } else {
                    videoViewHolder = (VideoViewHolder) convertView.getTag();
                }
                String filePathString = getItem(position).getFilePathString();
                if (filePathString!=null){
                    Glide.with(getContext())
                            .load(filePathString)           //地址
                            .placeholder(R.drawable.publicloading)  //加载图片
                            .error(R.drawable.icon_chat_talk_no)    //错误加载
                            .into(videoViewHolder.iv_video);        //指定给某个控件赋值
                }

                break;
            case Constant.PHOTO:
                //图片
                PhotoViewHolder photoViewHolder;
                if (convertView == null) {
                    convertView = inflater.inflate(R.layout.photo_item_layout, parent, false);
                    photoViewHolder = new PhotoViewHolder();
                    photoViewHolder.iv_photo = (ImageView) convertView.findViewById(R.id.iv_detail_photo);
                    convertView.setTag(photoViewHolder);
                } else {
                    photoViewHolder = (PhotoViewHolder) convertView.getTag();
                }
                String photoPath = getItem(position).getFilePathString();
                if (photoPath!=null){
                    Glide.with(getContext())
                            .load(photoPath)           //地址
                            .skipMemoryCache(true)      //跳过内存缓存,以免修改图片后无法更新
                            .placeholder(R.drawable.publicloading)  //加载图片
                            .error(R.drawable.icon_chat_talk_no)    //错误加载
                            .into(photoViewHolder.iv_photo);        //指定给某个控件赋值
                }
                break;
            case Constant.TEXT:
                //文字
                break;
        }
        return convertView;
    }

    class VoiceViewHolder {
        TextView seconds;// 时间
        View length;// 对话框长度
    }
    class VideoViewHolder{
        ImageView iv_video;
    }
    class PhotoViewHolder{
        ImageView iv_photo;
    }

}
